/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.interceptor;

import com.jspx.boot.environment.Environment;
import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.ActionProxy;
import com.jspx.txweb.dao.ActionLogDAO;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.IUserSession;
import com.jspx.txweb.online.OnlineManager;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.table.ActionLog;
import com.jspx.txweb.util.RequestUtil;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 11-8-21
 * Time: 下午9:51
 */
public class ActionLogInterceptor extends InterceptorSupport {
    private static final Logger log = LoggerFactory.getLogger(ActionLogInterceptor.class);

    private int needYear = 3;

    public int getNeedYear() {
        return needYear;
    }

    public void setNeedYear(int needYear) {
        this.needYear = needYear;
    }

    public void init() {

    }

    public void destroy() {

    }

    /**
     * 载入在线管理
     */
    private OnlineManager onlineManager;
    @Ref(name = Environment.onlineManager)
    public void setOnlineManager(OnlineManager onlineManager) {
        this.onlineManager = onlineManager;
    }

    private ActionLogDAO actionLogDAO;
    @Ref(name = Environment.actionLogDAO)
    public void setActionLogDAO(ActionLogDAO actionLogDAO) {
        this.actionLogDAO = actionLogDAO;
    }

    public String intercept(ActionInvocation actionInvocation) throws Exception {
        String result = actionInvocation.invoke();
        ActionProxy actionProxy = actionInvocation.getActionProxy();
        ActionSupport action = actionProxy.getAction();
        IUserSession userSession = onlineManager.getUserSession(action);
        //游客就不记录了
        if (action.isGuest()) {
            return result;
        }
         //也可以 return Action.ERROR; 终止action的运行
        //保存历史记录 begin
        //@method
        String operation = TXWebUtil.getInvokeMethod(action, actionProxy.getMethod());
        if (TXWebUtil.defaultExecute.equalsIgnoreCase(operation)&& !RequestUtil.isMultipart(action.getRequest())
                || "".equals(operation) || !StringUtil.hasLength(operation)) return result;
        String actionClassName = StringUtil.substringAfterLast(action.getClass().getName(), ".");
        if (actionClassName.endsWith("Action")) {
            ActionLog actionLog = action.getActionLog();
            if (!StringUtil.isNULL(actionLog.getContent())) {
                actionLogDAO.setNamespace(action.getRootNamespace());

                if (StringUtil.isNULL(actionLog.getTitle())) actionLog.setTitle(actionProxy.getCaption());
                if (StringUtil.isNULL(actionLog.getTitle())) actionLog.setTitle(operation);
                actionLog.setCaption(actionProxy.getCaption());
                actionLog.setClassMethod(operation);
                actionLog.setMethodCaption(actionProxy.getMethodCaption());
                actionLog.setNamespace(action.getRootNamespace());
                actionLog.setActionResult(result);
                long id;
                if (ClassUtil.isDeclaredMethod(action.getClass(), "getId"))
                    id = ObjectUtil.toLong(BeanUtil.getProperty(action, "getId"));
                else id = action.getLong("id");
                actionLog.setObjectId(id);
                actionLog.setUrl(action.getRequest().getRequestURI());
                actionLog.setPutName(userSession.getName());
                actionLog.setPutUid(userSession.getUid());
                actionLog.setIp(userSession.getIp());
                if (actionLogDAO.save(actionLog) < 0) {
                    log.error("日志记录保存发生错误");
                } else {
                    //1月1号删除
                    if (DateUtil.getMonth() == 1 && DateUtil.getDate() == 1) actionLogDAO.deleteYearBefore(needYear);
                }
                //删除3年前的记录数据
            }
        }
        //执行下一个动作,可能是下一个拦截器,也可能是action取决你的配置
        return result;
        //也可以 return Action.ERROR; 终止action的运行
    }
}